package com.vincce.sport.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.vincce.sport.core.utils.CheckUtil;
import com.vincce.sport.core.utils.ConvertUtil;
import com.vincce.sport.entity.CompetitionSeason;
import com.vincce.sport.entity.School;
import com.vincce.sport.entity.ScoreTaskEntity;
import com.vincce.sport.entity.Zone;
import com.vincce.sport.mapper.CompetitionSeasonMapper;
import com.vincce.sport.mapper.SchoolMapper;
import com.vincce.sport.mapper.ScoreTaskMapper;
import com.vincce.sport.mapper.TeamDateMapper;
import com.vincce.sport.mapper.TeamMapper;
import com.vincce.sport.mapper.ZoneMapper;

@Controller
@RequestMapping("frontScore")
public class ScoreController {

	@Resource
	private CompetitionSeasonMapper competitionSeasonMapper;
	@Resource
	private ZoneMapper zoneMapper;
	@Resource
	private SchoolMapper schoolMapper;
	@Resource
	private TeamMapper teamMapper;
	@Resource
	private TeamDateMapper teamDateMapper;
	@Resource
	private ScoreTaskMapper scoreTaskMapper;
	
	@RequestMapping("/show")
	public String show(HttpServletRequest request,Model model,
			String seasonId,String schoolId,String teamId,String zoneId,String _time){
		//获取赛季信息
		List<CompetitionSeason> seasonList = competitionSeasonMapper.querySeasonByAll();
		//获取赛区
		List<Zone> zoneList = zoneMapper.queryByZoneAll();
//		//获取学校
//		List<School> schoolList = schoolMapper.queryBySchoolAll();
//		//获取球队
//		List<Team> teamList = teamMapper.queryTeamByAll();
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("seasonId", seasonId == null ? "" : seasonId);
		param.put("schoolId", schoolId == null ? "" : schoolId);
		param.put("teamId", teamId == null ? "" : teamId);
		param.put("zoneId", zoneId == null ? "" : zoneId);
		param.put("_time", _time);
		//如搜索条件没有时或初始访问页面时，不显示数据
		if(CheckUtil.isEmpty(seasonId)  && CheckUtil.isEmpty(schoolId) && CheckUtil.isEmpty(teamId) && CheckUtil.isEmpty(zoneId) && CheckUtil.isEmpty(_time)){
			param.put("no_condition", "y");
//			param.put("_time", ConvertUtil.format("yyyy-MM-dd", new Date()));
			
		}
//		if(CheckUtil.isEmpty(_time)){
//			param.put("_time", new Date());
//		}
		//从t_score_task单表中获取数据
		List<ScoreTaskEntity> scoreTaskList = scoreTaskMapper.findByFrontScoreCondition(param);
		
		/*//获取主队数据
		List<TeamDate> datalist = teamDateMapper.queryTeamDateBy4condition(param);
		List teamIdTwo = new ArrayList();
		for (TeamDate teamDate : datalist) {
			teamIdTwo.add(teamDate.getTeamIdTwo());
		}
		List<TeamDate> opponentList = new ArrayList<TeamDate>();
		if(datalist.size() != 0){
			Map map = new HashMap();
			map.put("teamIds", teamIdTwo.toString().replace("[", "").replace("]", ""));
			//获取对手球队数据
			opponentList = teamDateMapper.queryTeamByOpponentId(map);
		}*/
		model.addAttribute("season", seasonList);
		model.addAttribute("zone", zoneList);
		model.addAttribute("scoreTaskList", scoreTaskList);
//		model.addAttribute("dataList", datalist);
//		model.addAttribute("opponentList", opponentList);
		//回显作用
		model.addAttribute("seasonId", seasonId);
		model.addAttribute("schoolId", schoolId);
		model.addAttribute("teamId", teamId);
		model.addAttribute("zoneId", zoneId);
		model.addAttribute("_time", _time);
		return "foreground/score";
	}
	
	@RequestMapping("querySchoolByzoneId")
	@ResponseBody
	public String querySchoolByzoneId(HttpServletRequest request,Integer zoneId){
		Map map = new HashMap();
		map.put("zoneId", zoneId);
		List<School> schoolList = schoolMapper.querySchoolByMap(map);
		return JSON.toJSONString(schoolList);
	}
	
	@RequestMapping("/updateScoreByTeamId")
	public void updateScoreByTeamId(HttpServletRequest request){
//		String teamId = request.getParameter("teamId");//球队ID
		String groupID = request.getParameter("groupID");//对应赛程ID
		Map<String,Object> map = new HashMap<String, Object>();
//		map.put("teamId", teamId);
		map.put("contestId", groupID);
		//获取主队数据
		List<ScoreTaskEntity> datalist = teamDateMapper.queryTeamDateByTeamAndContest(map);
		//List teamIdTwo = new ArrayList();
		//清空数据
		//scoreTaskMapper.deleete();
		Map<String,Object> param = new HashMap<String, Object>();
		for (ScoreTaskEntity scoreTask : datalist) {
			//teamIdTwo.add(scoreTask.getTeamIdTwo());
			param.put("teamTwoId", scoreTask.getTeamIdTwo());
			param.put("contestId", scoreTask.getContestId());
			//查找对手得分信息
			ScoreTaskEntity opponent = teamDateMapper.queryTeamDataToTaskByOpponentId(param);
			scoreTask.setFirstSectionScoreTwo(opponent.getFirstSectionScore());
			scoreTask.setSecondSectionScoreTwo(opponent.getSecondSectionScore());
			scoreTask.setThirdSectionScoreTwo(opponent.getThirdSectionScore());
			scoreTask.setFourthSectionScoreTwo(opponent.getFourthSectionScore());
			scoreTask.setOverScoreTwo(opponent.getOverScore());
			scoreTask.setTotalScoreTwo(opponent.getTotalScore());
			scoreTask.setTeamNameTwo(opponent.getTeamName());
			//插入临时比分表
			//判断该比赛比分已持久化
			//重新扫如数据
			scoreTaskMapper.insertScore(scoreTask);
			//logger.info("插入***t_score_task**主队名称："+scoreTask.getTeamName()+"**contest_id:"+scoreTask.getContestId());
		}
		System.out.println("扫描结束");
	}
}
